package com.ea.eadp.nexus.test.pageDefinition.console;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;

public class ConsoleBasePage extends BasePage {
	
	private String visibleElementId;
	private WebElement visibleElement;	
	
	public String getVisibleElementId() {
		return visibleElementId;
	}

	public WebElement getVisibleElement() {
		return visibleElement;
	}

	public ConsoleBasePage(WebDriver driver, Class<?> cla, String visibleElementId) {
		super(driver, cla);
		this.visibleElementId = visibleElementId;
	}
	
	public void sendInput(WebElement element, CharSequence keys) {
		element.sendKeys(keys);
	}
	
	public void sendInput(CharSequence keys) {
		this.visibleElement = driver.findElement(By.id(visibleElementId));
		sendInput(this.visibleElement,  keys);
	}

	public void waitPageLoadCompleted() {
		PageOperation.WebDriverWait(this.driver, visibleElementId);
		this.visibleElement = driver.findElement(By.id(visibleElementId));
	}

    public void validatePageLoadCompleted(String visibleElementId) {
        PageOperation.WebDriverWait(this.driver, visibleElementId);
    }

	public void sendControllerKey(char controllerKey) { 
		int keyCode = 0;
		switch(controllerKey) {
		case 'A':
			keyCode = 65;
			break;
		case 'B':
			keyCode = 66; 
			break;
		case 'X':
			keyCode = 88; 
			break;
		case 'Y':
			keyCode = 89; 
			break;
		}
		triggerKeyDownEvent(keyCode);
	}
	
	public void sendControllerKey(org.openqa.selenium.Keys key) {
		int keyCode = 0;
		switch(key) {
		case UP:
			keyCode = 38;
			break;
		case DOWN:
			keyCode = 40; 
			break;
		case LEFT:
			keyCode = 37; 
			break;
		case RIGHT:
			keyCode = 39; 
			break;
		}
		triggerKeyDownEvent(keyCode);
	}
	
	public void triggerKeyDownEvent(int keyCode) {
		if(keyCode != 0) {
			JavascriptExecutor jse = (JavascriptExecutor) driver;
			jse.executeScript("var press = $.Event('keydown');press.which = " + keyCode + ";$(document).trigger(press);");
		}
	}
	
	public void clearInput(String elementId) {
		JavascriptExecutor jse = (JavascriptExecutor) driver;
		jse.executeScript("$(\"#" + elementId +"\").val('');");
	}
}
